 // document.write("<script language='javascript' src='/js/model/Model.js?t=212'></script>");


// get     查询数量
// put       更新数据
// post     增加数据
// delete    删除数据

 
 // window.jsonpcallback  = function (response){
 // 	 console.info('jsonp result::', response)
 // };
 
function Model (header_lists = []){
this.baseUrl = '';//接口域名

this.jsonp = function  (obj,urltype = 0) {
     //定义一个处理Jsonp返回数据的回调函数
	 window["callback"] = function(object) {
		// console.info( object);
		//  console.info( JSON.parse(object) );
        obj.success(object);

     }
     var script = document.createElement("script");
     //组合请求URL
	 if(urltype == 1 ){
		   script.src = obj.url + "?callback=callback";
	 }else{
		   script.src = obj.url + "&callback=callback";
	 }
   
     for(key in obj.data){
         script.src +="&" + key + "=" + obj.data[key];
     }
     //将创建的新节点添加到BOM树上
     document.getElementsByTagName("body")[0].appendChild(script);  
 }

 


this.sendData = function (method = "GET",url = '',_data = {},cb){
//第一步：创建XMLHttpRequest对象
var xhr;
if (window.XMLHttpRequest) {            //非IE
xhr = new XMLHttpRequest();
//console.log('创建了XMLHttpRequest对象');
} else if (window.ActiveXObject) {       //IE
xhr = new ActiveXObject("Microsoft.XMLHTTP");
//console.log('IE');  //console.log('创建了ActiveXObject对象');
}
xhr.onreadystatechange =function(){
if(xhr.readyState == 4 && xhr.status == 200){
//获取服务器响应 //document.querySelector("#show").innerHTML = xhr.responseText;//return 
			console.info('请求接口完毕!...')
		 let data = JSON.parse(xhr.responseText)		
			cb(data)
}
};

 var urlEncode = function (param, key, encode) {  
   if(param==null) return '';  
   var paramStr = '';  
   var t = typeof (param);  
   if (t == 'string' || t == 'number' || t == 'boolean') {  
     paramStr += '&' + key + '=' + ((encode==null||encode) ? encodeURIComponent(param) : param);  
   } else {  
     for (var i in param) {  
       var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);  
       paramStr += urlEncode(param[i], k, encode);  
     }  
   }  
   return paramStr;  
 };  
  
  
  if(method == 'get'){
	    url = url + '?t=' + new Date().getTime() + urlEncode(_data); 
  }
  url = this.baseUrl + url;
//发送异步请求
xhr.open(method,url,true);
//xhr.setRequestHeader("token", new Date().getTime() );
xhr.setRequestHeader("Content-type","application/json");
//console.info('header_lists',header_lists );

 for (var i = 0; i < header_lists.length; i++) { 
	 xhr.setRequestHeader(header_lists[i].keys, header_lists[i].values);
 }

//发送请求.+
let _d;

console.table('数据格式 typeof ',typeof(_data) ,' instanceof Array',  (_data instanceof Array)  ,' instanceof object',  (_data instanceof Object)  );
//console.info('发送请求',_data );




if( typeof(_data) == 'string' ){
		_d = _data ; 
	console.info('typeof',_data );
} else if( typeof(_data) == 'object'  ){
	 if( _data instanceof FormData    ){
		 	let objData = {};
			_data.forEach((v, k) => { 
				  objData[k] = v;
			});
		    _d = JSON.stringify(objData);
	 }else{
		 _d = JSON.stringify(_data) ; 
	 }
	
		 
}else{
	
}



//return ;

if( method == 'post' || method == 'delete' ){
	
}
if(method == 'put'){

}
console.info('正在请求接口...')
xhr.send(_d) ;   // 请求体数据
};  

 


 
	
//================ 
}

 